package www.dashu.com.domain;

import www.dashu.com.domain.base.BaseVo;
import lombok.Data;
import org.hibernate.annotations.Formula;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Data
@Entity
@Table(name = "sys_user")
public class SysUser extends BaseVo implements Serializable
{

    private static final long serialVersionUID = 1L;

    @Id
    private Long id;

    @Column(name = "username")
    private String username;

    @Column(name = "password")
    private String password;

    @Column(name = "nickname")
    private String nickname;

    @Column(name = "phone")
    private String phone;

    @Column(name = "status")
    private Integer status;

    @Column(name = "icon")
    private String icon;

    @Column(name = "gender")
    private Integer gender;

    @Column(name = "source")
    private String source;

    @Column(name = "source_company")
    private Long company;

    @Column(name = "integration")
    private Integer integration;

    @Column(name = "growth")
    private Integer growth;

    @Column(name = "luckey_count")
    private Integer luckeyCount;

    @Column(name = "history_integration")
    private Integer historyIntegration;

    @Formula("(select s.name from sys_company s where s.id = source_company )")
    private String companyName;

    @Column(name = "create_time")
    private Date createTime;

    @Column(name = "update_time")
    private Date updateTime;

//    @Formula("(select concat(role.id,'_', role.name) as rname from sys_user_role m,sys_role role where m.r_id=role.id and m.u_id = id )")
    @Transient
    private List<String> roles;

    @Transient
    private List<String> appApis;

    @Transient
    private List<String> apps;

    @Override
    public String toString()
    {
        StringBuilder sb = new StringBuilder();
        sb.append(getClass().getSimpleName());
        sb.append(" [");
        sb.append("Hash = ").append(hashCode());
        sb.append(", id=").append(id);
        sb.append(", username=").append(username);
        sb.append(", password=").append(password);
        sb.append(", nickname=").append(nickname);
        sb.append(", phone=").append(phone);
        sb.append(", status=").append(status);
        sb.append(", icon=").append(icon);
        sb.append(", gender=").append(gender);
        sb.append(", source=").append(source);
        sb.append(", sourceCompany=").append(company);
        sb.append(", createTime=").append(createTime);
        sb.append(", updateTime=").append(updateTime);
        sb.append(", roles=").append(null == roles ? "[]" : roles.stream().map(String::valueOf).collect(Collectors.joining(", ")));
        sb.append("]");
        return sb.toString();
    }

}